#!/bin/bash

# Slackware build script for worldofpadman

# Copyright 2013 - Fernando Giannasi - São Paulo - Brasil
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# 20220427 bkw: Modified by SlackBuilds.org, BUILD=2:
# - don't install both 32-bit and 64-bit binaries (rm the ones
#   not needed for the current ARCH).
# - don't install windows .ico or mac .icns icons.
# - new-style icons (multiple sizes, plus scalable).
# - wrapper executables in /usr/games, so people can launch them
#   from the command line without knowing the full path in /opt.
# - fix .desktop file (this isn't a Qt nor KDE app).

cd $(dirname $0) ; CWD=$(pwd)

PRGNAM=worldofpadman
VERSION=${VERSION:-1.6}
BUILD=${BUILD:-2}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}
SRCVER=${SRCVER:-1.5}
PATCHVER=$VERSION

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i386 ;;
    x86_64) ARCH=x86_64 ;;
       *) echo "Architecture $(uname -m) not suported" && exit 1 ;;
  esac
fi

if [ ! -z "${PRINT_PACKAGE_NAME}" ]; then
  echo "$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE"
  exit 0
fi

TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

# No flags/configure needed as it is just a binary repackaging.

set -e 

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
mkdir -p $PKG/opt/$PRGNAM
cd $PKG/opt/$PRGNAM
unzip $CWD/*$SRCVER-unified.zip

# Patch the new version if available
if [ -f $CWD/*$PATCHVER-patch-unified.zip ]; then
  unzip -o $CWD/*$PATCHVER-patch-unified.zip
fi

cd $PKG
chown -R root:root .

# 20220428 bkw: fix permissions, remove what we don't need:
find . -type d -exec chmod 0755 {} \+
find . -type f -exec chmod 0644 {} \+
cd $PKG/opt/$PRGNAM
if [ "$ARCH" = "x86_64" ]; then
  rm -f *i386*
else
  rm -f *x86_64*
fi
rm -rf *.exe *.dll wop.app # windows and mac stuff, don't need
chmod +x *.so wop.* wopded.*

# 20220428 bkw: XTRAS is mostly documentation, plus icons.
# interestingly enough, the documentation includes the source.
cd XTRAS

for px in 16 32 48 64 128; do
  size=${px}x${px}
  dir=$PKG/usr/share/icons/hicolor/$size/apps
  mkdir -p $dir
  convert -background none -geometry $size icon.svg $dir/$PRGNAM.png
done
mkdir -p $PKG/usr/share/icons/hicolor/scalable/apps
mv icon.svg $PKG/usr/share/icons/hicolor/scalable/apps/$PRGNAM.svg

# 20220428 bkw: don't need windows and mac icons.
rm -f *.ico *.icns

# 20220428 bkw: what's left is just docs, move it to correct Slackware dir.
cd ..
PKGDOC=$PKG/usr/doc/$PRGNAM-$VERSION
mkdir -p $PKGDOC
mv XTRAS/* $PKGDOC
rm -rf XTRAS
cat $CWD/$PRGNAM.SlackBuild > $PKGDOC/$PRGNAM.SlackBuild

# Install a .desktop launcher:
mkdir -p $PKG/usr/share/applications
cat $CWD/worldofpadman.desktop > $PKG/usr/share/applications/worldofpadman.desktop

# 20220428 bkw: wrapper scripts. this allows CLI users to run the game
# or server by typing "wop" or "wopded", and keeps us from having to
# change the .desktop file for different ARCHes.
mkdir -p $PKG/usr/games
for exe in wop wopded; do
  sed -e "s,@ARCH@,$ARCH,g" -e "s,@EXE@,$exe,g" \
    < $CWD/wrapper.sh \
    > $PKG/usr/games/$exe
  chmod 0755 $PKG/usr/games/$exe
done

# 20220428 bkw: for compatibility with previous version of this script:
ln -s wop $PKG/usr/games/padman

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/doinst.sh > $PKG/install/doinst.sh

# Pack it all
cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE
